package com.xydtech.common.utils;

import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.deepoove.poi.XWPFTemplate;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;


/**
 * @author yjs
 * @version 1.0
 * @description: 文件格式转换工具类
 * @date 2023年10月25日 8:55
 */

public class WordConvertPng {

    public static boolean getLicense() {
        boolean result = false;
        InputStream is = null;
        try {
            Resource resource = new ClassPathResource("license.xml");
            is = resource.getInputStream();
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static boolean turnPng(String inPath, String outPath) {
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return false;
        }
        FileOutputStream os = null;
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一个空白文档
            os = new FileOutputStream(file);
            Document doc = new Document(inPath);
            // 设置字体,需要把Windows系统c:\windows\fonts路径内的所有文件拷贝到linux服务器的/usr/share/fonts/winFonts内
            FontSettings.setFontsFolder("/usr/share/fonts/winFonts", true);
            doc.save(os, SaveFormat.PNG);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,PNG
            // EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            File infile = new File(inPath);
            infile.delete();
            System.out.println("pdf转换成功，共耗时：" + ((now - old) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

   public static void main(String[] arg){
       //turnPng("E:\\1111.docx","E:\\222.png");

        //doc2pdf(docPath,pdfPath);
       try {
           /**组装合同模板参数数据*/
           Map<String, Object> datas = new HashMap<>();
           datas.put("cName","王老吉");
           datas.put("amount","100000元");
           datas.put("manager","王依依");
           datas.put("phone","13235330330");
           datas.put("zhName","兖州农商银行城区支行");
           datas.put("data","2024年5月20日");
           datas.put("address","济宁高新区王因街道办");
           //生成合同
           XWPFTemplate template = XWPFTemplate.compile("E:\\212.docx").render(datas);
           FileOutputStream out;
           String downloadPath ="E:\\2222.docx";
           out = new FileOutputStream(downloadPath);
           template.write(out);
           out.flush();
           out.close();
           template.close();
           String pdfPath = "E:\\3333.png";
           File file = new File(pdfPath);
           File fileParent = file.getParentFile();
           if (!fileParent.exists()) {
               fileParent.mkdirs();
           }

           //Windows系统使用(documents4转换)
           //Pdf2Word.documents4jWordToPdf(downloadPath, pdfPath);
           turnPng(downloadPath, pdfPath);

       } catch (Exception e) {
           System.out.println(e.getMessage());
       }

    }
}
